<?php
                session_start();
                $sessionid = session_id();
                $servername = "localhost";
                $username = "root";
                $password = "wgz52157";
                $dbname = "myFirstDB";
                //规定每组人数
                $maxgroupper = 4;
                
                $insertgroupnum = 1;
                $user_name = "用户1";
                $conn = new mysqli($servername, $username, $password, $dbname);
                $nowgroupnum = mysqli_query($conn, "SELECT ANY_VALUE(choosenum),groupnum,count(*)AS COUNT FROM MyUsers GROUP BY groupnum ORDER BY groupnum DESC");
                while ($row = mysqli_fetch_array($nowgroupnum)) {
                    if ($row['COUNT'] < $maxgroupper && $row['ANY_VALUE(choosenum)'] ==-1) {
                        $insertgroupnum = $row['groupnum'];
                        $user_name = substr($user_name, 0, strlen($user_name) - 1);
                        $number = $row['COUNT'] + 1;
                        $user_name .= $number;
                        $sql = "INSERT INTO MyUsers (id,usernamenum, groupnum, allincome,choosenum,roundnum)
   VALUES ('{$sessionid}','{$user_name}', '{$insertgroupnum}', '10','-1','0')";
                        if (mysqli_query($conn, $sql)) {
                            // echo "新记录插入成功"."<br>";
                        } else {
                            echo "Error: " . $sql . "<br>" . mysqli_error($conn);
                        }
                        break;
                    }
                    else{
                        $insertgroupnum = $row['groupnum']+1;
                        $sql = "INSERT INTO MyUsers (id,usernamenum, groupnum, allincome,choosenum,roundnum)
   VALUES ('{$sessionid}','{$user_name}', '{$insertgroupnum}', '10','-1','0')";
                        if (mysqli_query($conn, $sql)) {
                            // echo "新记录插入成功"."<br>";
                        } else {
                            echo "Error: " . $sql . "<br>" . mysqli_error($conn);
                        }
                        break;
                    }
                }
                $sql = "INSERT INTO MyUsers (id,usernamenum, groupnum, allincome,choosenum,roundnum)
   VALUES ('{$sessionid}','{$user_name}', '{$insertgroupnum}', '10','-1','0')";
                        if (mysqli_query($conn, $sql)) {
                            // echo "新记录插入成功"."<br>";
                        } else {
                            echo "Error: " . $sql . "<br>" . mysqli_error($conn);
                        }
?>